package angle;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import common.BaseAction;
import entities.TFunction;
import entities.TOperator;
import entities.TRole;
import interfaces.IFunctionService;
import interfaces.IOperatorService;
import interfaces.IOxfService;
import interfaces.IResourceService;
import interfaces.IRoleService;
import interfaces.IRxfService;
import util.IpUtil;
import util.LogUtil;
import utils.AesUtil;
import utils.StringUtil;

public class RofAngle extends BaseAction {

	private static final long serialVersionUID = 7507534255609348887L;

	private IOperatorService operatorService;
	private IRoleService roleService;
	private IFunctionService functionService;
	private IOxfService oxfService;
	private IRxfService rxfService;
	private IResourceService resourceService;

	private TOperator operatorBean;
	private List<TOperator> listoperator;
	private String optNo;
	private String catNo;
	private String optId;
	private String optName;
	private String optPwd;
	private String optPicture;
	private String optPoint;
	private String optState;
	private Date optDate;
	private String optRemark;

	private TRole roleBean;
	private List<TRole> roleList;
	private String rolNo;
	private String rolName;
	private String rolState;
	private Date rolDate;
	private String rolRemark;

	private TFunction functionBean;
	private List<TFunction> listfunction;
	private String funNo;
	private String funName;
	private String funDad;
	private String funRedirect;
	private Integer funSeq;
	private String funEnable;
	private String funIcon;
	private String funState;
	private Date funDate;
	private String funRemark;
	private String funTree;

	private Date optDateTop;
	private Date optDateEnd;
	private Date rolDateTop;
	private Date rolDateEnd;
	private Date funDateTop;
	private Date funDateEnd;
	private static Map<String, Integer> operatorMap = new HashMap<String, Integer>();

	// 新增或者修改入口
	// 根据主键获取对象,为空则创建带主键新对象
	public String login() throws Exception {
		String ip = IpUtil.getAddress(ServletActionContext.getRequest());
		Map<String, Object> session = ServletActionContext.getContext().getSession();

		if (!ServletActionContext.getContext().getSession().containsKey("code")) {
			addActionError("验证码失效,请刷新");
			return ERROR;
		}
		if (StringUtil.isEmpty(optId) || StringUtil.isEmpty(optPwd)) {
			addActionError("用户名和密码不能为空");
			return ERROR;
		}
		if (StringUtil.isEmpty(code) || !code.equals(ServletActionContext.getContext().getSession().get("code").toString())) {
			addActionError("验证码错误");
			return ERROR;
		}
		// 检测登录用户是否已经map内
		if (operatorMap.containsKey(optId + "_" + ip)) {
			int count = (Integer) operatorMap.get(optId + "_" + ip);
			if (count > 6) {
				addActionError("尝试登录次数太多,您已经被禁止访问,请联系管理人员解锁");
				return ERROR;
			}
		}

		// 查询操作员
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap = new HashMap<String, Object>();
		paramMap.put("optId", optId);
		paramMap.put("optPwd", AesUtil.AES_Encrypt(AesUtil.KEY, optPwd));
		operatorBean = operatorService.single(paramMap);
		session.put("operatorBean", operatorBean);

		// 登录次数统计
		if (operatorBean == null) {
			if (operatorMap.containsKey(optId + "_" + ip)) {
				int count = (Integer) operatorMap.get(optId + "_" + ip);
				operatorMap.put(optId + "_" + ip, count + 1);
				LogUtil.login("[" + ip + "]使用用户名[" + optId + "]密码[" + optPwd + "]尝试登录" + (count + 1) + "次失败");
			} else {
				operatorMap.put(optId + "_" + ip, 1);
				LogUtil.login("[" + ip + "]使用用户名[" + optId + "]密码[" + optPwd + "]尝试登录1次失败");
			}
			addActionError("用户名或者密码错误");
			return ERROR;
		}

		// 查询关联功能列表
		QuerySet<Object> querySet = new QuerySet<Object>();
		paramMap = new HashMap<String, Object>();
		paramMap.put("optNo", new String[] { operatorBean.getOptNo() });
		querySet.setParamMap(paramMap);
		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		session.put("operatorlistfunction", oxfService.relate(querySet).getResultList());

		// 查询操作员资源控制
		paramMap = new HashMap<String, Object>();
		paramMap.put("optNo", new String[] { operatorBean.getOptNo() });
		querySet.setParamMap(paramMap);
		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		session.put("operatorResourceList", resourceService.relate(querySet).getResultList());
		return SUCCESS;
	}

	public String test() throws Exception {
		Map<String, Object> session = ServletActionContext.getContext().getSession();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("optId", "000000");
		paramMap.put("optPwd", AesUtil.AES_Encrypt(AesUtil.KEY, "Errors1980"));
		operatorBean = operatorService.single(paramMap);
		session.put("operatorBean", operatorBean);

		// 查询关联功能列表
		QuerySet<Object> querySet = new QuerySet<Object>();
		paramMap = new HashMap<String, Object>();
		paramMap.put("optNo", new String[] { operatorBean.getOptNo() });
		querySet.setParamMap(paramMap);
		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		session.put("operatorlistfunction", oxfService.relate(querySet).getResultList());

		paramMap = new HashMap<String, Object>();
		paramMap.put("optNo", new String[] { operatorBean.getOptNo() });
		querySet.setParamMap(paramMap);
		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		session.put("operatorResourceList", resourceService.relate(querySet).getResultList());
		return SUCCESS;
	}

	public String logout() throws Exception {
		ServletActionContext.getContext().getSession().clear();
		return SUCCESS;
	}

	////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////
	//////////////////////////////////////////////////////

	public IOperatorService getOperatorService() {
		return operatorService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public IFunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}

	public IOxfService getOxfService() {
		return oxfService;
	}

	public void setOxfService(IOxfService oxfService) {
		this.oxfService = oxfService;
	}

	public IRxfService getRxfService() {
		return rxfService;
	}

	public void setRxfService(IRxfService rxfService) {
		this.rxfService = rxfService;
	}

	public IResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(IResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public TOperator getOperatorBean() {
		return operatorBean;
	}

	public void setOperatorBean(TOperator operatorBean) {
		this.operatorBean = operatorBean;
	}

	public List<TOperator> getlistoperator() {
		return listoperator;
	}

	public void setlistoperator(List<TOperator> listoperator) {
		this.listoperator = listoperator;
	}

	public String getOptNo() {
		return optNo;
	}

	public void setOptNo(String optNo) {
		this.optNo = optNo;
	}

	public String getCatNo() {
		return catNo;
	}

	public void setCatNo(String catNo) {
		this.catNo = catNo;
	}

	public String getOptId() {
		return optId;
	}

	public void setOptId(String optId) {
		this.optId = optId;
	}

	public String getOptName() {
		return optName;
	}

	public void setOptName(String optName) {
		this.optName = optName;
	}

	public String getOptPwd() {
		return optPwd;
	}

	public void setOptPwd(String optPwd) {
		this.optPwd = optPwd;
	}

	public String getOptPicture() {
		return optPicture;
	}

	public void setOptPicture(String optPicture) {
		this.optPicture = optPicture;
	}

	public String getOptPoint() {
		return optPoint;
	}

	public void setOptPoint(String optPoint) {
		this.optPoint = optPoint;
	}

	public String getOptState() {
		return optState;
	}

	public void setOptState(String optState) {
		this.optState = optState;
	}

	public Date getOptDate() {
		return optDate;
	}

	public void setOptDate(Date optDate) {
		this.optDate = optDate;
	}

	public String getOptRemark() {
		return optRemark;
	}

	public void setOptRemark(String optRemark) {
		this.optRemark = optRemark;
	}

	public TRole getRoleBean() {
		return roleBean;
	}

	public void setRoleBean(TRole roleBean) {
		this.roleBean = roleBean;
	}

	public List<TRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<TRole> roleList) {
		this.roleList = roleList;
	}

	public String getRolNo() {
		return rolNo;
	}

	public void setRolNo(String rolNo) {
		this.rolNo = rolNo;
	}

	public String getRolName() {
		return rolName;
	}

	public void setRolName(String rolName) {
		this.rolName = rolName;
	}

	public String getRolState() {
		return rolState;
	}

	public void setRolState(String rolState) {
		this.rolState = rolState;
	}

	public Date getRolDate() {
		return rolDate;
	}

	public void setRolDate(Date rolDate) {
		this.rolDate = rolDate;
	}

	public String getRolRemark() {
		return rolRemark;
	}

	public void setRolRemark(String rolRemark) {
		this.rolRemark = rolRemark;
	}

	public TFunction getFunctionBean() {
		return functionBean;
	}

	public void setFunctionBean(TFunction functionBean) {
		this.functionBean = functionBean;
	}

	public List<TFunction> getlistfunction() {
		return listfunction;
	}

	public void setlistfunction(List<TFunction> listfunction) {
		this.listfunction = listfunction;
	}

	public String getFunNo() {
		return funNo;
	}

	public void setFunNo(String funNo) {
		this.funNo = funNo;
	}

	public String getFunName() {
		return funName;
	}

	public void setFunName(String funName) {
		this.funName = funName;
	}

	public String getFunDad() {
		return funDad;
	}

	public void setFunDad(String funDad) {
		this.funDad = funDad;
	}

	public String getFunRedirect() {
		return funRedirect;
	}

	public void setFunRedirect(String funRedirect) {
		this.funRedirect = funRedirect;
	}

	public Integer getFunSeq() {
		return funSeq;
	}

	public void setFunSeq(Integer funSeq) {
		this.funSeq = funSeq;
	}

	public String getFunEnable() {
		return funEnable;
	}

	public void setFunEnable(String funEnable) {
		this.funEnable = funEnable;
	}

	public String getFunIcon() {
		return funIcon;
	}

	public void setFunIcon(String funIcon) {
		this.funIcon = funIcon;
	}

	public String getFunState() {
		return funState;
	}

	public void setFunState(String funState) {
		this.funState = funState;
	}

	public Date getFunDate() {
		return funDate;
	}

	public void setFunDate(Date funDate) {
		this.funDate = funDate;
	}

	public String getFunRemark() {
		return funRemark;
	}

	public void setFunRemark(String funRemark) {
		this.funRemark = funRemark;
	}

	public String getFunTree() {
		return funTree;
	}

	public void setFunTree(String funTree) {
		this.funTree = funTree;
	}

	public Date getOptDateTop() {
		return optDateTop;
	}

	public void setOptDateTop(Date optDateTop) {
		this.optDateTop = optDateTop;
	}

	public Date getOptDateEnd() {
		return optDateEnd;
	}

	public void setOptDateEnd(Date optDateEnd) {
		this.optDateEnd = optDateEnd;
	}

	public Date getRolDateTop() {
		return rolDateTop;
	}

	public void setRolDateTop(Date rolDateTop) {
		this.rolDateTop = rolDateTop;
	}

	public Date getRolDateEnd() {
		return rolDateEnd;
	}

	public void setRolDateEnd(Date rolDateEnd) {
		this.rolDateEnd = rolDateEnd;
	}

	public Date getFunDateTop() {
		return funDateTop;
	}

	public void setFunDateTop(Date funDateTop) {
		this.funDateTop = funDateTop;
	}

	public Date getFunDateEnd() {
		return funDateEnd;
	}

	public void setFunDateEnd(Date funDateEnd) {
		this.funDateEnd = funDateEnd;
	}

}
